public class QuadraticEquation {
    private double a, b, c; // 方程的系数

    // 构造函数
    public QuadraticEquation(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    // 求解方程
    public void solve() {
        if (a == 0) {
            if (b == 0) {
                System.out.println("方程无解。");
            } else {
                double root = -c / b;
                System.out.println("方程为线性方程，解为: " + root);
            }
            return;
        }

        double discriminant = b * b - 4 * a * c; // 计算判别式

        if (discriminant > 0) {
            double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
            double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
            System.out.println("方程有两个不同的实根: " + root1 + " 和 " + root2);
        } else if (discriminant == 0) {
            double root = -b / (2 * a);
            System.out.println("方程有一个重根: " + root);
        } else {
            double realPart = -b / (2 * a);
            double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
            System.out.println("方程有两个复根: " + realPart + " + " + imaginaryPart + "i 和 " + realPart + " - " + imaginaryPart + "i");
        }
    }

    // 主方法用于测试
    public static void main(String[] args) {
        QuadraticEquation equation1 = new QuadraticEquation(1, -3, 2);
        equation1.solve(); // 方程 x^2 - 3x + 2 = 0

        QuadraticEquation equation2 = new QuadraticEquation(1, 2, 1);
        equation2.solve(); // 方程 x^2 + 2x + 1 = 0

        QuadraticEquation equation3 = new QuadraticEquation(1, 0, -4);
        equation3.solve(); // 方程 x^2 - 4 = 0

        QuadraticEquation equation4 = new QuadraticEquation(0, 2, 4);
        equation4.solve(); // 方程 2x + 4 = 0
    }
}
